package cn.wolfcode.wolf2w.service.impl;


import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyTheme;
import cn.wolfcode.wolf2w.query.StrategyThemeQuery;
import cn.wolfcode.wolf2w.repository.StrategyRepository;
import cn.wolfcode.wolf2w.repository.StrategyThemeRepository;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StrategyThemeServiceImpl implements IStrategyThemeService {


    @Autowired
    private MongoTemplate template;
    @Autowired
    private StrategyThemeRepository repository;
    @Override
    //page对象里面包括: 总页数 每页显示条数......
    public Page<StrategyTheme> query(StrategyThemeQuery qo) {

        //PageResult 7核心属性
        //核心: count  list
        //使用template方式只能返回list, 需要将list分装成pagte对象

        //1: 查询总数:total
        Query query = new Query();
        long total = template.count(query, StrategyTheme.class);
        if (total == 0) {
            return Page.empty();  //当查询条数为0时则返回为空
        }
        //2: 设置分页相关数据 以及返回数据
        PageRequest pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
                Sort.Direction.DESC, "_id");
        query.with(pageable);
        List<StrategyTheme> list = template.find(query, StrategyTheme.class);

        //3:封装page对象
        return new PageImpl<StrategyTheme>(list, pageable, total);
    }

    @Override
    public void saveOrUpdate(Strategy strategy) {

    }

    public List<StrategyTheme> list() {
        return repository.findAll();
    }

    @Override
    public Object findAll() {
        return repository.findAll();
    }
}
